/* Copyright 2012 University of Ulm

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package de.uulm.inki.arqtest;
import junit.framework.TestSuite;
import arq.qtest;
import arq.cmd.TerminationException;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.ARQ;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.engine.main.StageBuilder;
import com.hp.hpl.jena.sparql.engine.main.StageGenerator;
import com.hp.hpl.jena.sparql.expr.E_Function;
import com.hp.hpl.jena.sparql.expr.NodeValue;
import com.hp.hpl.jena.sparql.junit.EarlReport;
import com.hp.hpl.jena.sparql.junit.ScriptTestSuiteFactory;
import com.hp.hpl.jena.sparql.junit.SimpleTestRunner;
import com.hp.hpl.jena.sparql.util.NodeFactory;
import com.hp.hpl.jena.sparql.util.Symbol;
import com.hp.hpl.jena.sparql.vocabulary.DOAP;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;
import com.hp.hpl.jena.sparql.vocabulary.TestManifest;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.XSD;


public class TestRunner extends qtest {
    public static final String ENT="http://www.w3.org/ns/entailment/";
    public static final String OWLDIRECT_NS=ENT+"OWL-Direct";
    public static final String OWLRDFBASED_NS=ENT+"OWL-RDF-Based";
    public static final String D_NS=ENT+"D";
    public static final String RDF_NS=ENT+"RDF";
    public static final String RDFS_NS=ENT+"RDFS";
    
    // include only tests that have a name starting with one of the given string
    // leave empty for all tests
    public static final String[] includeOnlyTests=new String[] {  };//"sparqldl-02"
    // generates just the entailment tests with OWL Direct Semantics
    //public static final String[] includeOnlySemantics=new String[] { RDF_NS, RDFS_NS, D_NS, OWLRDFBASED_NS };
    public static final String[] includeOnlySemantics=new String[] { RDF_NS, RDFS_NS, D_NS };
    
    // http://www.w3.org/2009/sparql/docs/tests/data-sparql11/entailment/manifest.ttl
    
    public TestRunner(String[] argv) {
        super(argv);
    }

    public static void main (String... argv) {
        //ARQ.hideNonDistiguishedVariables
        StageGenerator orig=(StageGenerator)ARQ.getContext().get(ARQ.stageGenerator);
        StageGenerator inferenceStageGenerator=new InferenceStageGenerator(orig);
        StageBuilder.setGenerator(ARQ.getContext(), inferenceStageGenerator);
        ARQ.getContext().set(ARQ.optimization, false);
        ARQ.getContext().set(ARQ.strictGraph, true);
        ARQ.getContext().set(ARQ.strictSPARQL, true);
        ARQ.getContext().set(Symbol.create("Semantics"), "RDFS");
        ARQ.init();
        try {
            new TestRunner(argv).mainRun();
        } catch (TerminationException ex) { 
            System.exit(ex.getCode()); 
        }
    }
    @Override
    protected void exec() {
        NodeValue.VerboseWarnings=false;
        E_Function.WarnOnUnknownFunction=false;
        if (createEarlReport)
            oneManifestEarl(testfileAbs) ;
        else
            oneManifest(testfileAbs) ;
    }
    static void oneManifest(String testManifest) {
        TestSuite suite=new InferenceScriptTestSuitFactory().process(testManifest);// generates just the entailment tests with OWL Direct Semantics
        junit.textui.TestRunner.run(suite) ;
    }
    static void oneManifestEarl(String testManifest) {
        String name="ARQ+Inference";
        String releaseName =  "ARQ+Inference" ;
        String version = "0.1" ;
        String homepage = "http://code.google.com/p/arq-inference/" ;
        String systemURI = "http://code.google.com/p/arq-inference/" ;  
        
        EarlReport report=new EarlReport(systemURI, name, version, homepage) ;
        ScriptTestSuiteFactory.results=report ;
        Model model=report.getModel() ;
        model.setNsPrefix("dawg", TestManifest.getURI()) ;
        // Birte runs the report
        Resource who=model.createResource(FOAF.Person)
                .addProperty(FOAF.name, "Birte Glimm")
                .addProperty(FOAF.homepage, 
                             model.createResource("http://www.uni-ulm.de/in/ki/glimm")) ;
        Resource reporter=report.getReporter() ;
        reporter.addProperty(DC.creator, who) ;
        model.setNsPrefix("doap", DOAP.getURI()) ; 
        model.setNsPrefix("xsd", XSD.getURI()) ;
        // DAWG specific stuff.
        Resource system=report.getSystem() ;
        system.addProperty(RDF.type, DOAP.Project) ;
        system.addProperty(DOAP.name, name) ;
        system.addProperty(DOAP.homepage, homepage) ;
        system.addProperty(DOAP.maintainer, who) ;
        
        Resource release=model.createResource(DOAP.Version) ;
        system.addProperty(DOAP.release, release) ;
        
        Node today_node=NodeFactory.todayAsDate() ;
        Literal today=model.createTypedLiteral(today_node.getLiteralLexicalForm(), today_node.getLiteralDatatype()) ;
        release.addProperty(DOAP.created, today) ;
        release.addProperty(DOAP.name, releaseName) ;      // Again
        
        TestSuite suite=new InferenceScriptTestSuitFactory().process(testManifest);
        SimpleTestRunner.runSilent(suite) ;
        
        InferenceScriptTestSuitFactory.results.getModel().write(System.out, "TTL") ;
    }
}
